package com.ruoyi.bigScreen.service.impl;


import com.ruoyi.bigScreen.mapper.BigScreenDataQualityMapper;
import com.ruoyi.bigScreen.domain.vo.BigScreenDataQualityVO;
import com.ruoyi.bigScreen.domain.vo.BigScreenRzsWrStatvolHwrVO;
import com.ruoyi.bigScreen.service.IBigScreenDataQualityService;
import com.ruoyi.bigScreen.util.ArithmeticUtils;
import com.ruoyi.common.core.domain.business.waterDistribution.AnnualTaskProgress;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 数据质量完整率记录Service业务层处理
 *
 * @author hwd
 * @date 2023-02-16
 */
@Service
public class BigScreenDataQualityServiceImpl implements IBigScreenDataQualityService {

    @Autowired
    private BigScreenDataQualityMapper bigScreenDataQualityMapper;

    @Override
    public List<BigScreenDataQualityVO> listStationDataWzl(BigScreenDataQualityVO BigScreenDataQualityVO) throws ParseException {

        //如果没有选择时间，默认为当前年月
        if (BigScreenDataQualityVO.getQueryTime()==null){
            BigScreenDataQualityVO.setQueryTime(new Date());
        }
        String [] strings= DateUtils.jsYmd(BigScreenDataQualityVO.getQueryTime());
        String yy =strings[0];
        String mm =strings[1];
        BigScreenDataQualityVO.setYy(yy);
        BigScreenDataQualityVO.setMm(mm);

        //查询水管机构级别
        HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(BigScreenDataQualityVO.getOrgCd());
            int grade = (int) addvAndOrgCodeInfo.get("grade");
            String resCode = (String) addvAndOrgCodeInfo.get("resCode");
            BigScreenDataQualityVO.setOrgCd(resCode + "%");
        if (grade == 1) {
            //查找下属水管机构
            resCode = resCode + "%0000";
        } else if (grade == 2) {
            //查找下属水管机构
            resCode = resCode + "%00";
        } else if (grade == 3) {
            resCode = resCode + "%";
        } else {
            resCode = resCode;
        }
        //根据水管机构和时间查询测站实报次数，上报频率
        List<BigScreenDataQualityVO> BigScreenDataQualityVOList= bigScreenDataQualityMapper.selBigScreenStStationRSbCount(BigScreenDataQualityVO);
        //查找下属机构
        BigScreenRzsWrStatvolHwrVO rzsWrStatvolHwrVO=new BigScreenRzsWrStatvolHwrVO();
        rzsWrStatvolHwrVO.setOrgCd(resCode);
        List<BigScreenRzsWrStatvolHwrVO> orglist = bigScreenDataQualityMapper.listOrgCd(rzsWrStatvolHwrVO);
            List<BigScreenDataQualityVO> rateVoList=new ArrayList<>();
            for (BigScreenRzsWrStatvolHwrVO r:orglist
                 ) {
                HashMap<String, Object> stringObjectHashMap = BusinessUtils.getAddvAndOrgCodeInfo(r.getOrgCd());
                String ro =  (String) stringObjectHashMap.get("resCode");
                //过滤后只查所属水管机构的测站
                List<BigScreenDataQualityVO> filterBigScreenDataQualityVOList = BigScreenDataQualityVOList.stream().filter(s -> s.getOrgCd().startsWith(ro)).collect(Collectors.toList());
                BigScreenDataQualityVO newBigScreenDataQualityVO=new BigScreenDataQualityVO();
                int sbCount =0;
                int ybCount =0;
                for (BigScreenDataQualityVO d:filterBigScreenDataQualityVOList
                     ) {
                    sbCount+=d.getSbCount();
                    //通过查询时间和上报频次计算应报次数
                    ybCount+=countEQ(BigScreenDataQualityVO.getQueryTime(),d.getStatFreq());
                }
                newBigScreenDataQualityVO.setSbCount(sbCount);
                newBigScreenDataQualityVO.setYbCount(ybCount);
                newBigScreenDataQualityVO.setStatCd(r.getStatCd());
                newBigScreenDataQualityVO.setOrgCd(r.getOrgCd());
                newBigScreenDataQualityVO.setOrgNm(r.getOrgNm());
                if (ybCount==0){
                    newBigScreenDataQualityVO.setIntegrityRate(0.0);
                }else {
                    newBigScreenDataQualityVO.setIntegrityRate(ArithmeticUtils.mul(ArithmeticUtils.div(sbCount,ybCount,3),100,3));
                }
                if (newBigScreenDataQualityVO.getIntegrityRate()>100){
                    newBigScreenDataQualityVO.setIntegrityRate(100.0);
                }
                rateVoList.add(newBigScreenDataQualityVO);
            }

        return rateVoList;

    }


    /**
     * 应报次数：	无线
     * 雷达遥测水位计	每5分钟1次 2
     * 	雷达水位计	每小时1次  1
     * 	电子水尺	每5分钟1次  3
     * @param date 时间
     * @return
     */
    private Integer countEQString(String date,Integer sbcount) throws ParseException {
        Long s=jsStringDate(date,sbcount);
        return s.intValue();
    }

    /**
     * 根据时间、上报频率计算应报次数
     * @param date
     * @param sbcount
     * @return
     * @throws ParseException
     */
    private static Long jsStringDate(String date,Integer sbcount) throws ParseException {
        long s = 0;
        if (sbcount==0){
            return s;
        }
        int n = date.length()-date.replaceAll("-", "").length();
        Date startDate = new Date();
        Date endDate = new Date();
        //计算当天开始到结束的应报次数
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd");
        if (n==1){
            String dates[] =date.split("-");

            //获得当前月第一天
            startDate=DateUtils.getDay(startSdf.parse(DateUtils.getFirstDayOfMonth1(Integer.parseInt(dates[0]),Integer.parseInt(dates[1]))));
            //获得当前月最后一天
            endDate = DateUtils.getDay(startSdf.parse(DateUtils.getLastDayOfMonth1(Integer.parseInt(dates[0]),Integer.parseInt(dates[1]))));
        }else {
            //获得当前月第一天
            startDate =DateUtils.getDay(startSdf.parse(date));
            //获得当前月最后一天
            endDate = DateUtils.getDayEnd(startSdf.parse(date));
        }

        SimpleDateFormat Sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String start = Sdf.format(startDate);
        String end = Sdf.format(endDate);
        if (!endDate.before(new Date())){
            //说明时间已超过当前时间，设置为当前时间
            Date nowDate=new Date();
            Calendar calendarnow = Calendar.getInstance();
            calendarnow.setTime(nowDate);
            end= Sdf.format(calendarnow.getTime());
            s=1;
        }
        long time1 = DateUtils.dateLongToStamp(start);
        long time2 =DateUtils.dateLongToStamp(end);
        s = s+(time2 - time1) / (1000 * 60) / sbcount;
        if (s<0){
            s=0;
        }
        return s;
    }


    /**
     * 应报次数：	无线雷达遥测水位计	每5分钟1次 2
     * 	雷达水位计	每小时1次  1
     * 	电子水尺	每5分钟1次  3
     * @param date 时间
     * @return
     */
    private static Integer countEQ(Date date,Integer sbcount) throws ParseException {
        Long s=jsdate(date,sbcount);
        return s.intValue();
    }

    /**
     * 根据时间、上报频率计算应报次数
     * @param date
     * @param sbcount
     * @return
     * @throws ParseException
     */
    private static Long jsdate(Date date,Integer sbcount) throws ParseException {
        long s = 0;
        if (sbcount==0){
            return s;
        }
        //计算当前月开始到结束的应报次数
        SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获得当前月第一天
        Date startDate =DateUtils.getMonthStart(date);
        //获得当前月最后一天
        Date endDate = DateUtils.getMonthEnd(date);
        SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start = startSdf.format(startDate);
        String end = endSdf.format(endDate);
        if (!endDate.before(new Date())){
            //说明时间已超过当前时间，设置为当前时间之后的1小时
            Date nowDate=new Date();
            Calendar calendarnow = Calendar.getInstance();
            calendarnow.setTime(nowDate);
            end= startSdf.format(calendarnow.getTime());
            s=1;
        }
        long time1 = DateUtils.dateLongToStamp(start);
        long time2 =DateUtils.dateLongToStamp(end);

        s = s+(time2 - time1) / (1000 * 60) / sbcount;
        if (s<0){
            s=0;
        }
        return s;
    }


    public static void main(String[] args) throws ParseException {
       System.out.println(countEQ(new Date(),60));
    }


}
